package lz.controls
{
	import com.common.utils.DisplayObjectUtil;
	import com.common.utils.Reflection;
	import com.global.GlobalDef;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	import flash.system.LoaderContext;
	
	import lz.controls.core.UIComponent;

	/**
	 *图像容器 
	 * @author lake
	 */
	public class ImageContain extends UIComponent
	{
		private var _url:String;
		
		private var _source:Object;
		
		private var _loader:Loader = new Loader();
		
		private var dowmloadTime:Number = 0;
		
		private var _bitmapData:BitmapData;
		
		public function ImageContain()
		{
			super();
			this.mouseChildren=false;
			this.mouseEnabled=false;
		}
		/**
		 *设置资源 数据 
		 * @param value
		 * 
		 */		
		public function set source(value:Object):void
		{
			clear();
			_source = value;
			if(_source == null)
			{
				clear();
			}	
			else if(value is BitmapData)
			{
				DisplayObjectUtil.removeAllChildren(this);
				var bitmap:Bitmap = new Bitmap(value as BitmapData);
				bitmap.width = this.width;
				bitmap.height = this.height;
				this.addChild(bitmap);
			}
			else if(value is String && value != "")
			{
				var mc:* = Reflection.createInstance(value as String);
				if(mc)
				{
//					mc.width = this.width;
//					mc.height = this.height;
					this.addChild(mc);
				}
			}
		}
		/**
		 * source
		 * @return 
		 * 
		 */		
		public function get source():Object
		{
			return _source;
		}
		/**
		 *资源位置 
		 * @param value
		 * 
		 */		
		public function set url(value:String):void
		{
			if(value == "" || value == null)
			{
				_url=null;
				clear();
			}
			else
			{
				if(_url == value)
				{
					return;
				}
				clear();
				_url = value;
				load(_url);
			}
		}
		/**
		 * url
		 * @return 
		 * 
		 */		
		public function get url():String
		{
			return _url;
		}
		/**
		 * 
		 * clear
		 */		
		public function clear():void
		{
			DisplayObjectUtil.removeAllChildren(this);
			unload();
			_source = null;
			_url=null;
			dowmloadTime = 0;
			if(_bitmapData != null)
			{
				_bitmapData.dispose();	
				_bitmapData = null;
			}
		}
		/**
		 * 
		 * unload
		 */		
		private function unload():void
		{
			try
			{
				_loader.close();
			}
			catch(e:Error)
			{
				
			}
			_loader.unloadAndStop();
			_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,completeHandler);
			//_loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
		}
		/**
		 * load
		 * @param url
		 * 
		 */		
		private function load(url:String):void
		{
			var request:URLRequest = new URLRequest(url);
			var content:LoaderContext = new LoaderContext(true,null,null);
			_loader.load(request,content);
			
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeHandler);
			_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
			_loader.contentLoaderInfo.addEventListener(Event.OPEN,openHandler);
		} 
		/**
		 * openHandler
		 * @param e
		 * 
		 */		
		private function openHandler(e:Event):void
		{
			this.dispatchEvent(new Event(Event.OPEN));
		}
		/**
		 * ioErrorHandler
		 * @param e
		 * 
		 */		
		private function ioErrorHandler(e:IOErrorEvent):void
		{
			_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,completeHandler);
			_loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
			_loader.contentLoaderInfo.removeEventListener(Event.OPEN,openHandler);
			dowmloadTime++;
			if(dowmloadTime >= 3)
			{
				//trace("error:",url);
				if(this.hasEventListener(IOErrorEvent.IO_ERROR) == false)
				{
					//trace("error:",url);
					//var turl :String = "http://thirdapp1.qlogo.cn/qzopenapp/a77546418354b602b2ddc624cb8bdbb3edb25d19a9d1b0a8d79f1e6ca0e207b0/50";
					var turl :String = GlobalDef.Web_Res_Url + "star/default.png";
					var request:URLRequest = new URLRequest(turl);
					var content:LoaderContext = new LoaderContext(true,null,null);
					_loader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeHandler);
					_loader.load(request,content);
				}
				else
				{
					this.dispatchEvent(new IOErrorEvent(IOErrorEvent.IO_ERROR));
				}
			}
			else
			{
				if(_url != null)
				{
					load(url);
				}
			}
		}
		/**
		 *加载完毕 
		 * @param e
		 */		
		private function completeHandler(e:Event):void
		{
			_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,completeHandler);
			_loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
			_loader.contentLoaderInfo.removeEventListener(Event.OPEN,openHandler);
			try
			{
				_bitmapData = Bitmap(e.target.content).bitmapData;
				var bitmap:Bitmap = Bitmap(e.target.content);
				bitmap.width = (width == 0?bitmap.width:width);
				bitmap.height = (height == 0?bitmap.height:height);
				addChild(bitmap);
				unload();
			}catch(e :Error)//策略文件问题，不能访问资源内容 
			{
				this.addChild(_loader);
				_loader.width = (width == 0?_loader.width:width);
				_loader.height = (height == 0?_loader.height:height);
			}
			this.dispatchEvent(new Event(Event.COMPLETE));
		}
		/**
		 *获取位图数据 
		 */		
		public function get bitmapData():BitmapData
		{
			if(_bitmapData == null)
			{
				
			}
			return _bitmapData;
		}
	}
}